import { defineStore, StoreDefinition } from "pinia";
import { ref } from "vue";
import type { Ref } from "vue";
import type { Banner, Brand, Category, HotItem, NewItem, Product, Special } from "@/types/homeTypes/homeTypes.ts";
import type { homeStoreInterface } from "@/pinia/modules/homeStore/Interface.ts";
import { fetchBannerListAPI, fetchBrandListAPI, fetchCategoryListAPI, fetchHotListAPI, fetchNewListAPI, fetchProductListAPI, fetchSpecialListAPI } from "@/api/homeApi/homeApi.ts";

const useHomeStore = defineStore("homeStore", (): homeStoreInterface => {
   /*分类列表*/
   const categoryList: Ref<Category[]> = ref<Category[]>([] as Category[]);
   /*品牌列表*/
   const brandList: Ref<Brand[]> = ref<Brand[]>([]);
   /*轮播图列表*/
   const bannerList: Ref<Banner[]> = ref<Banner[]>([]);
   /*新鲜好物列表*/
   const newList: Ref<NewItem[]> = ref<NewItem[]>([]);
   /*人气推荐列表*/
   const hotList: Ref<HotItem[]> = ref<HotItem[]>([]);
   /*热门品牌列表*/
   const hotBrandList: Ref<Brand[]> = ref<Brand[]>([]);
   /*产品区块*/
   const productList: Ref<Product[]> = ref<Product[]>([]);
   /*最新专题*/
   const specialList: Ref<Special[]> = ref<Special[]>([]);
   /*获取分类列表的请求函数*/
   const fetchCategoryList = async (): Promise<void> => {
      const { result }: { result: Category[] } = await fetchCategoryListAPI();
      categoryList.value = result;
   };
   /*获取品牌列表的请求函数*/
   const fetchBrandList = async (limit: number): Promise<void> => {
      const { result }: { result: Brand[] } = await fetchBrandListAPI(limit);
      brandList.value = result;
   };
   /*获取轮播图的请求函数*/
   const fetchBannerList = async (): Promise<void> => {
      const { result }: { result: Banner[] } = await fetchBannerListAPI();
      bannerList.value = result;
   };
   /*获取新鲜好物的请求函数*/
   const fetchNewList = async (): Promise<void> => {
      const { result }: { result: NewItem[] } = await fetchNewListAPI();
      newList.value = result;
   };
   /*获取人气推荐的请求函数*/
   const fetchHotList = async (): Promise<void> => {
      const { result }: { result: HotItem[] } = await fetchHotListAPI();
      hotList.value = result;
   };
   /*获取热门品牌的请求函数*/
   const fetchHotBrandList = async (limit: number): Promise<void> => {
      const { result }: { result: Brand[] } = await fetchBrandListAPI(limit);
      hotBrandList.value = result;
   };
   /*获取产品区块的请求函数*/
   const fetchProductList = async (): Promise<void> => {
      const { result }: { result: Product[] } = await fetchProductListAPI();
      productList.value = result;
   };
   /*获取最新专题的请求函数*/
   const fetchSpecialList = async (limit: number): Promise<void> => {
      const { result }: { result: Special[] } = await fetchSpecialListAPI(limit);
      specialList.value = result;
   };
   return {
      categoryList,
      fetchCategoryList,
      brandList,
      fetchBrandList,
      bannerList,
      fetchBannerList,
      newList,
      fetchNewList,
      hotList,
      fetchHotList,
      hotBrandList,
      fetchHotBrandList,
      productList,
      fetchProductList,
      specialList,
      fetchSpecialList,
   };
});

export default useHomeStore;
